<?

class Report_model extends Model {
	
	function __construct()
	{
		parent::Model();
		
		$this->load->database();
	}
	
	function insert($mode, $product_display_id, $delivery, $backlog)
	{
		$data = array(
			"delivery" => $delivery,
			"backlog" => $backlog
		);
		
		$result = TRUE;
		if ("ADD" == $mode)
		{
			$data["product_display_id"] = $product_display_id;
			if ($this->db->insert('imp_rpt_production', $data) === FALSE) 
				$result = FALSE;
		}
		else if ("EDIT" == $mode)
		{
			$this->db->where("product_display_id", $product_display_id);
			if ($this->db->update('imp_rpt_production', $data) === FALSE)
				$result = FALSE;
		}
		
		db_log_message($this->db->last_query());
		return $result;
	}
	
	function update_stock_kg_need($product_display_id, $stock_unit, $total_kg_need)
	{
		$data = array(
			"stock_unit" => $stock_unit,
			"total_kg_need" => $total_kg_need
		);
		
		$this->db->where("product_display_id", $product_display_id);
		$this->db->update("imp_rpt_production", $data);
	}
	
	function update_actual_slitted($currentDate, $coil_group_code, $slittedCoil, $slittedWeight)
	{
		$data = array(
			"slitted_coil" => $slittedCoil,
			"slitted_weight" => $slittedWeight
		);
		
		$this->db->where("coil_group_code", $coil_group_code);
		$this->db->where("actual_slitted_date", $currentDate);
		$this->db->update("prd_rpt_actual_slitted", $data);
		
		//echo $this->db->last_query();
	}
	
	function insert_actual_slitted($currentDate,$coil_group_code, $slittedCoil, $slittedWeight)
	{
		$data = array(
			"actual_slitted_date" => $currentDate,
			"coil_group_code" => $coil_group_code,
			"slitted_coil" => $slittedCoil,
			"slitted_weight" => $slittedWeight
		);
		
		$this->db->insert("prd_rpt_actual_slitted", $data);
		//echo $this->db->last_query();
		
	}
	
	
	function get_vr_report()
	{
		$query = $this->db->get("vr_rpt_production");
		
		db_log_message($this->db->last_query());
		$result = array();
		foreach($query->result_array() as $item)
		{
			$result[] = $item;
		}
		
		return $result;
	}
	
	
	
	function get_vr_overall_stock_report()
	{
		$query = $this->db->get("vr_rpt_overal");
		
		db_log_message($this->db->last_query());
		$result = array();
		foreach($query->result_array() as $item)
		{
			$result[] = $item;
		}
		
		return $result;
	}
	
	function get_all_coil_by_date($startDate, $endDate)
	{
		$this->db->where("actual_slitted_date <=" , $endDate);
		$this->db->where("actual_slitted_date >=" , $startDate);
		$this->db->select_sum("slitted_coil");
		$this->db->select_sum("slitted_weight");
		
		$query = $this->db->get("prd_rpt_actual_slitted");
		
		/// echo $this->db->last_query();
		if ($query->num_rows() > 0)
		{
			$temp = $query->row_array();
			return array("slitted_coil" => $temp["slitted_coil"] , "slitted_weight" => $temp["slitted_weight"]);
		}
		
		return array("slitted_coil" => 0 , "slitted_weight" => 0);
	}

	
	function get_vr_finishgood_detail_report($sort = 'sortDate', $sort_type = 'desc')
	{
		$this->db->order_by($sort, $sort_type);
		$this->db->order_by('sortDate', 'desc');
		$this->db->order_by('machine', 'desc');		
		$query = $this->db->get("vr_rpt_finishgood_detail");
		
		//echo $this->db->last_query();
		
		db_log_message($this->db->last_query());
		$result = array();
		foreach($query->result_array() as $item)
		{
			$result[] = $item;
		}
		
		return $result;	
	}
	
	function get_date_diff_group_by_machine($startDate, $endDate)
	{
		$sql = "SELECT DISTINCT mc_id, COUNT( * ) AS Total FROM  `mc_machine_work_hour` WHERE work_date BETWEEN '" . $startDate . "' AND '" .$endDate . "'  GROUP BY mc_id";
		
		db_log_message($this->db->last_query());
		
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
		
		return array();
	}
	
	function get_vr_finishgood_report()
	{
		$query = $this->db->get("vr_rpt_finishgood");
		
		db_log_message($this->db->last_query());
		$result = array();
		foreach($query->result_array() as $item)
		{
			$result[] = $item;
		}
		
		return $result;	
	}
	
	function get_vr_coil_received_report($sort = 'sortReceivedDate', $sort_type = 'desc')
	{
		$this->db->order_by($sort, $sort_type);
		$this->db->order_by('sortReceivedDate', 'desc');
		$query = $this->db->get("vr_rpt_coil_received");
		
		//echo $this->db->last_query();
		
		db_log_message($this->db->last_query());
		
		$result = array();
		foreach($query->result_array() as $item)
		{
			$result[] = $item;
		}
		
		return $result;
		
		
	}
	
	function get_vr_slit_report($sort = 'sortSlitDate', $sort_type = 'desc')
	{
		$this->db->order_by($sort, $sort_type);	
		$this->db->order_by('sortSlitDate', 'desc');	
		$query = $this->db->get("vr_rpt_slit");
		
		//echo $this->db->last_query();
		
		db_log_message($this->db->last_query());
		$result = array();
		foreach($query->result_array() as $item)
		{
			$result[] = $item;
		}
		
		return $result;	
		
	}
	
	
	function clear()
	{
		$this->db->truncate("imp_rpt_production");
	}
	
}